#include<iostream>
#include<vector>
using namespace std;

vector<int> f(1010, 0);

int findfa(int x) {
    return f[x] = f[x] == x ? x : findfa(f[x]);
}

int main() {
    int n, m;
    while(cin >> n) {
        int cnt = 0;
        if(n == 0)  break;
        cin >> m;
        for(int i=1; i<=n; i++) f[i] = i;
        while(m--) {
            int a, b;
            cin >> a >> b;
            a = findfa(a);
            b = findfa(b);
            f[a] = b;
        }
        for(int i=1; i<=n; i++) {
            if(f[i] == i) cnt++;
        }
        cout << cnt - 1 << endl;
    }
    return 0;
} 